import flash.events.MouseEvent;
class VideoPlayer extends flash.display.Sprite
{
	var ns:flash.net.NetStream;

	var header : flash.text.TextField;
	var player: flash.media.Video;
	var startBtn:fl.controls.Button; 
	var stopBtn:fl.controls.Button; 
	var pauseBtn:fl.controls.Button; 
	var logOutput : fl.controls.TextArea;
	var background:flash.display.Sprite;
	
	var log:String->Void;
	var streamName:String;
	
	public function new(nc:flash.net.NetConnection, streamName, log)
	{
		super();
		
		addEventListener(flash.events.Event.ADDED_TO_STAGE, onAddedToStage);
		
		this.streamName = streamName;
		this.log = log;
		
		createView();
		if(nc.connected)
		{
			ns = new flash.net.NetStream(nc);
			//ns.bufferTime = 2.0;
			ns.client=this;
			ns.addEventListener(flash.events.NetStatusEvent.NET_STATUS, onStatus);
			ns.addEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, onAsyncError); 
			ns.play(streamName);
			
			player.attachNetStream(ns);
			
		}
		else
		{
			log("NetConnection is no longer connected");
		}
	}
	function onAsyncError(event:flash.events.AsyncErrorEvent):Void
	{
		log(this.streamName+": ns async) text="+event.text+", error=" + event.error);
	}
	function onStatus(event:flash.events.NetStatusEvent):Void
	{
		log(this.streamName+": ns status) code="+event.info.code+ if(event.info.description!=null)", description=" + event.info.description else "");
		log(ns.info.toString().split(" ").join("\n"));
		logLocal(event.info.code);
	}
	function onMetaData(data)
	{
		log(this.streamName+": ns meta");
		for(i in Reflect.fields(data))
			log(i+"="+Reflect.field(data, i));
		if(data.width>0 && data.height>0)
			resize(data.width, data.height);
		var info = ns.info;
	}
	
	function onStreamStart(event:MouseEvent):Void
	{
		ns.play(streamName);
		
		startBtn.enabled=false;
		stopBtn.enabled=true;
		pauseBtn.enabled=true;
		startBtn.label = "playing...";
	}
	function onStreamStop(event:MouseEvent):Void
	{
		ns.play(false);
		
		startBtn.enabled=true;
		stopBtn.enabled=false;
		pauseBtn.enabled=false;
		startBtn.label = "play";
	}
	function onStreamPause(event:MouseEvent):Void
	{
		ns.pause();
		
		startBtn.enabled=false;
		stopBtn.enabled=true;
		pauseBtn.enabled=true;
	}
	function onWindowClose(event:MouseEvent):Void
	{
		ns.play(false);
		ns.removeEventListener(flash.events.NetStatusEvent.NET_STATUS, onStatus);
		ns.removeEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
		parent.removeChild(this);
	}
	function onStartDrag(event:MouseEvent):Void
	{
		flash.Lib.current.stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		event.currentTarget.parent.parent.setChildIndex(event.currentTarget.parent, event.currentTarget.parent.parent.numChildren-1);
		event.currentTarget.parent.startDrag();
	}
	function onStopDrag(event:MouseEvent):Void
	{
		this.stopDrag();
		flash.Lib.current.stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
	}
	function onAddedToStage(?event=null)
	{
		removeEventListener(flash.events.Event.ADDED_TO_STAGE, onAddedToStage);
		positionMiddle(this);
	}
	function createView()
	{
		background = new flash.display.Sprite();
		background.buttonMode=true;
		addChild(background);
		background.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
		
		header = new flash.text.TextField();
		header.mouseEnabled=false;
		header.text = "Live Stream: "+streamName;
		addChild(header);
		
		player = new flash.media.Video();
		addChild(player);

		/*closeBtn = new fl.controls.Button(); 
		closeBtn.label = "close";
		closeBtn.addEventListener(MouseEvent.CLICK, onWindowClose);
		addChild(closeBtn);
		*/
		
		startBtn = new fl.controls.Button(); 
		startBtn.label = "play";
		startBtn.enabled=false;
		startBtn.addEventListener(MouseEvent.CLICK, onStreamStart);
		addChild(startBtn);
		
		stopBtn = new fl.controls.Button(); 
		stopBtn.label = "stop";
		stopBtn.enabled=true;
		stopBtn.addEventListener(MouseEvent.CLICK, onStreamStop);
		addChild(stopBtn);
		
		pauseBtn = new fl.controls.Button(); 
		pauseBtn.label = "pause";
		pauseBtn.enabled=true;
		pauseBtn.addEventListener(MouseEvent.CLICK, onStreamPause);
		addChild(pauseBtn);
		
		logOutput = new fl.controls.TextArea();
		logOutput.enabled=false;
		logOutput.mouseEnabled=false;
		addChild(logOutput);
		
		resize(player.width, player.height);
	}
	function resize(w, h):Void
	{
		header.x =
		player.x =
		startBtn.x = 
		stopBtn.x = 
		pauseBtn.x =
		logOutput.x = 
		logOutput.x = 10;
		
		header.width =
		player.width =
		startBtn.width = 
		stopBtn.width = 
		pauseBtn.width = 
		logOutput.width = w;
		
		header.height = 20;
		player.height = h;
		logOutput.height = 80;
		
		header.y = 10;
		player.y = header.y + header.height + 10;
		startBtn.y = player.y + player.height + 10;
		stopBtn.y = startBtn.y + startBtn.height + 10;
		pauseBtn.y = stopBtn.y + stopBtn.height + 10;
		logOutput.y = pauseBtn.y + pauseBtn.height + 10;

		background.graphics.clear();
		background.graphics.beginFill(0xDDDDEE, 1.0);
		background.graphics.drawRoundRect(0,0,player.width+20,logOutput.y + logOutput.height+10, 10,10);
		
		positionMiddle(this);
	}
	function logLocal(msg:Dynamic)
	{
		logOutput.text += msg+"\n";
		logOutput.textField.scrollV = logOutput.textField.maxScrollV;
	}
	function positionMiddle(obj:Dynamic)
	{
		obj.x = (flash.Lib.current.stage.stageWidth - obj.width)/2;
		obj.y = (flash.Lib.current.stage.stageHeight - obj.height)/2;
	}
	
}
